package com.tbit.uqbike.webmanager.controller;

import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.common.constant.OperationPlatform;
import com.tbit.common.entity.UserOpLog;
import com.tbit.common.enums.OperaPswEnum;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.object.pojo.AccountUser;
import com.tbit.uqbike.object.pojo.User;
import com.tbit.uqbike.object.pojo.dto.*;
import com.tbit.uqbike.object.pojo.vo.RideOrderRefundRpcReqVO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.*;
import com.tbit.uqbike.webmanager.business.user_op_log.service.UserOpLogService;
import com.tbit.uqbike.webmanager.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * 退款操作
 *
 * @Author:BUGTian
 * @DATE: 2022/11/28
 */
@RestController
@RequestMapping("/refundOperation")
public class RefundController {

    @Autowired
    private RefundService refundService;
    @Autowired
    private UserOpLogService userOpLogService;
    @Autowired
    private UserService userService;
    @Autowired
    private AccountUserService accountUserService;
    @Autowired
    private OperaPswService operaPswService;

    /**
     * 骑行订单退款
     */
    @PutMapping("/rideOrderRefund")
    @OperateLog(module = ModuleEnum.REFUND, methods = MethodEnum.RIDE_ORDER_REFUND_OPERATION)
    public Result<Integer> rideOrderRefund(HttpServletRequest request, @RequestBody RideOrderRefundDTO refundDTO) {
        LoginInfo loginUser = BaseController.getLogin(request);
        /**操作密码校验*/
        operaPswService.checkOperaPsw(loginUser, OperaPswEnum.OPERA_PSW_ORDER_CONFIG.getModule(), refundDTO.getPsw());

        Result<Integer> result = refundService.rideOrderRefund(loginUser, refundDTO);
        if (!result.unSuccess()) {
            // 插入用户操作日志
            User user = userService.getByUserId(refundDTO.getUserId());
            userOpLogService.insertSingle(UserOpLog.builder()
                    .accountId(user.getAccountId())
                    .opUser(loginUser.getName())
                    .opUserPhone(loginUser.getPhone())
                    .targetUser(user.getName())
                    .targetUserPhone(user.getPhone())
                    .operation("订单退款")
                    .opType("退款")
                    .opPlatform(OperationPlatform.MANAGER.code)
                    .opTime(LocalDateTime.now())
                    .detail("骑行订单号: " + refundDTO.getOrderNO() + "\n变动金额: " + (refundDTO.getRefundMoney() + refundDTO.getFineMoney()) / 100d)
                    .build());
        }
        return result;
    }

    /**
     * 骑行订单退款（提供远程调用）
     */
    @PutMapping("/rpc/rideOrderRefund")
    public Result<Integer> rideOrderRefund(@RequestBody RideOrderRefundRpcReqVO reqVO) {
        Result<Integer> result = refundService.rideOrderRefund(reqVO);
        if (!result.unSuccess()) {
            // 插入用户操作日志
            AccountUser accountUser = accountUserService.getAccountUserById(reqVO.getAccountUserId());
            User user = userService.getByUserId(reqVO.getUserId());
            userOpLogService.insertSingle(UserOpLog.builder()
                    .accountId(user.getAccountId())
                    .opUser(accountUser.getName())
                    .opUserPhone(accountUser.getPhone())
                    .targetUser(user.getName())
                    .targetUserPhone(user.getPhone())
                    .operation("订单退款")
                    .opType("退款")
                    .opPlatform(OperationPlatform.MAINTAIN.code)
                    .opTime(LocalDateTime.now())
                    .detail("骑行订单号: " + reqVO.getOrderNO() + "\n变动金额: -" + (reqVO.getRefundMoney() + reqVO.getFineMoney()) / 100d)
                    .build());
        }
        return result;
    }

    /**
     * 骑行卡、免押卡、会员卡、优惠券套餐退款
     */
    @PutMapping("/orderRefund")
    @OperateLog(module = ModuleEnum.REFUND, methods = MethodEnum.ORDER_REFUND_OPERATION)
    public Result<Integer> orderRefund(HttpServletRequest request, @RequestBody OrderRefundDTO orderRefundDTO) {

        /**操作密码校验*/
        operaPswService.checkOperaPsw(BaseController.getLogin(request), OperaPswEnum.OPERA_PSW_MONEY_CONFIG.getModule(),
                    orderRefundDTO.getPsw());

        return refundService.orderRefund(BaseController.getLogin(request), orderRefundDTO);
    }


    /**
     * 用户余额退款
     */
    @PutMapping("/userMoneyRefund")
    @OperateLog(module = ModuleEnum.REFUND, methods = MethodEnum.USER_MONEY_REFUND_OPERATION)
    public Result<RefundReturnDTO> userMoneyRefund(HttpServletRequest request, @RequestBody UserMoneyRefundDTO userMoneyRefundDTO) {
        /**操作密码校验*/
        operaPswService.checkOperaPsw(BaseController.getLogin(request), OperaPswEnum.OPERA_PSW_MONEY_CONFIG.getModule(),
                    userMoneyRefundDTO.getPsw());

        Result<RefundReturnDTO> result = refundService.userMoneyRefund(BaseController.getLogin(request), userMoneyRefundDTO);
        return result;
    }

    /**
     * 押金退款
     */
    @PutMapping("/depositRefund")
    @OperateLog(module = ModuleEnum.REFUND, methods = MethodEnum.DEPOSIT_REFUND_OPERATION)
    public Result<Integer> depositRefund(HttpServletRequest request, @RequestBody DepositRefundDTO depositRefundDTO) {
        /**操作密码校验*/
        operaPswService.checkOperaPsw(BaseController.getLogin(request), OperaPswEnum.OPERA_PSW_MONEY_CONFIG.getModule(),
                    depositRefundDTO.getPsw());

        Result<Integer> result = refundService.depositRefund(BaseController.getLogin(request), depositRefundDTO);
        return result;
    }

    /**
     * @description: 退款结果查询
     * @author: mo.shanyong
     * @date: 2024/12/19 10:37
     * @param: refundBusinessNO
     * @return: Result<com.tbit.uqbike.object.pojo.dto.RefundReturnDTO>
     */
    @GetMapping("/queryRefundResult")
    public Result<RefundReturnDTO> queryRefundResult(String refundBusinessNO){
        Assert.hasLength(refundBusinessNO,"查询退款订单号为空");
        return refundService.queryRefundResult(refundBusinessNO);
    }



}
